define(['jquery', 'bootstrap', 'backend', 'table', 'form','jstree'], function ($, undefined, Backend, Table, Form,undefined) {

    var Controller = {
    		
        chooseproject:function(){
            
            
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var chooseTable = $('#chooseTable')
            
            $('#choose_id').click(function(){
                //let id =  Controller.api.selectedids(chooseTable);
            	let row = chooseTable.bootstrapTable('getSelections');
            	if(!row.length){
            		Toastr.error("请选择项目");
            		return
            	}
                Fast.api.close(row[0]);
            })
        	
        	$.ajax({
                url: "information/u8fitem/getItemData",
                type: 'post',
                dataType: 'json',
                data: {cItem:$("#cItem").val()},
                success: function (ret) {
                	console.log(ret)
                    if (ret.hasOwnProperty("code")) {
                        var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
                        if (ret.code === 1) {
                            //销毁已有的节点树
                            $("#treeview").jstree("destroy");
                            Controller.api.rendertree(data);
                        } else {
                            Backend.api.toastr.error(ret.msg);
                        }
                    }
                }, error: function (e) {
                    Backend.api.toastr.error(e.message);
                }
            })
            
            chooseTable.bootstrapTable({
                url: "information/u8fitem/chooseproject" + location.search,
                pk: 'citemcode',
                search:false,
                singleSelect:true,
                //sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'citemccode', title: __('大类明细'), operate: 'LIKE',visible:false},
                        {field: 'citemcode', title: __('项目编码'), operate: 'LIKE'},
                        {field: 'citemname', title: __('项目名称'), operate: 'LIKE'}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(chooseTable);

        },
        api: {
            //获取选中的行
            selectedids: function (table, current) {
                var options = table.bootstrapTable('getOptions');
                //如果有设置翻页记忆模式
                if (!current && options.maintainSelected) {
                    return options.selectedIds;
                }
                return $.map(table.bootstrapTable('getSelections'), function (row) {
                    return row[options.pk];
                });
            },
            rendertree: function (content) {
                $("#treeview")
                    .on('redraw.jstree', function (e) {
                        $(".layer-footer").attr("domrefresh", Math.random());
                    }).on('changed.jstree', function (e, data) {
                          let citemccode = data.node.id;
                          $("[name='citemccode']").val(citemccode);
                          $("[type='submit']").trigger('click')

                   }).jstree({
                        "plugins": [ "themes",'types'],
                        "core": {
                            'check_callback': true,
                            "data": content
                        }
                    });
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
            
        }
    };
    return Controller;
});
